﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

using System.Text;
using Frame.Lib.Core.Module;
using Frame.Lib.Common;


public partial class Frame_Left : System.Web.UI.Page
{
    protected int UpperID
    {
        get
        {
            return int.Parse(Request["UpperID"]);
        }
    }  

    //直接定位到的模块的ID
    protected string ModuleID
    {
        get
        {
            return Request["ModuleID"];
        }
    }

    private ModuleBO module
    {
        get
        {
            return ModuleBO.Instance<ModuleBO>();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {           
            //litHtml.EnableViewState = false;
            //litHtml.Text = BuildHTML();

            Response.Write(BuildJSON());
            Response.End();
        }
    }

    private string BuildJSON()
    {
        //超时判断
   
        if (PageBase.IsTimeout())
        {
            return "timeout";
        }

        List<int> subIDs = module.GetSubIDs(UpperID);
        IEnumerable<ModuleInfo> list = module.GetList(subIDs, AppSession.UserID).Where(T => T.State == (int)ModuleState.Enable);

        StringBuilder sbHtml = new StringBuilder();
        sbHtml.Append("var modules = [");
        //二级菜单
        IEnumerable<ModuleInfo> menu2 = list.Where(data => data.UpperID == UpperID);
        foreach (ModuleInfo info in menu2)
        {
            sbHtml.Append("{ID:'" + info.ID + "',Name:'" + info.Name + "',NavUrl:'" + info.NavUrlAbsPath + "',SubModules:[");
            //三级菜单
            IEnumerable<ModuleInfo> menu3 = list.Where(data => data.UpperID == info.ID);
            foreach (ModuleInfo info3 in menu3)
            {
                sbHtml.Append("{ID:'" + info3.ID + "',Name:'" + info3.Name + "',NavUrl:'" + info3.NavUrlAbsPath + "',IconUrl:'" + info3.IconUrlAbsPath + "'}");
                if (info3 != menu3.Last())
                {
                    sbHtml.Append(",");
                }
            }
            sbHtml.Append("]},");
        }

        string strJSON = sbHtml.ToString().TrimEnd(',') + "]";

        return strJSON;

    }

    private string BuildHTML()
    {
        List<int> subIDs = module.GetSubIDs(UpperID);
        List<ModuleInfo> list = module.GetList(subIDs, AppSession.UserID);

        StringBuilder sbHTML = new StringBuilder();
        //二级菜单
        IEnumerable<ModuleInfo> menu2 = list.Where(data => data.UpperID == UpperID);
        foreach (ModuleInfo info in menu2)
        {
            sbHTML.Append("<div class='module2' style=''><a href='javascript:void(0);' class='collapse' id='menu" + info.ID + "'  onclick='expend(this,\"sub" + info.ID + "\");'>&nbsp;</a><a href='javascript:go()' hidefocus='hidefocus' onclick='navModule2(this,\"" + info.NavUrlAbsPath + "\")'>" + info.Name + "</a></div>\n");

            sbHTML.Append("<div id = 'sub" + info.ID + "' style='display:none; '>\n");
            //三级菜单
            IEnumerable<ModuleInfo> menu3 = list.Where(data => data.UpperID == info.ID);
            foreach (ModuleInfo info3 in menu3)
            {
                sbHTML.Append("<div class='module3'><a href='javascript:go()' id='menu" + info3.ID + "'  hidefocus='hidefocus' onmouseover='mouseOver(this)' onmouseout='mouseOut(this)' onclick='navModule3(this,\"" + info3.NavUrlAbsPath + "\");'><b style ='background-image:url(" + info3.IconUrlAbsPath + ");'></b><span id='span" + info3.ID + "'>" + info3.Name + "</span></a></div>\n");
            }

            sbHTML.Append("</div>\n");
        }

        if (menu2.Count() > 0)
        {

            
            string firstModuleID;
            if (ModuleID != null && ModuleID != "")
            {
                firstModuleID = ModuleID;
            }
            else
            {
                firstModuleID = menu2.First().ID.ToString();
            }
            sbHTML.Append("<input type='hidden' id='firstModuleID' value='" + firstModuleID + "' />");
            //string strScript = "initLeft('" + firstModuleID + "');";
            //body.Attributes.Add("onload", strScript);
        }


        return sbHTML.ToString();
    }

   
}
